﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace SharpDiary.Core.Controls
{
    [DefaultEvent("Search")]
    public partial class SearchTextBox : UserControl
    {
        private GraphicsPath _gPath = new GraphicsPath();
        private Color _borderColor = Color.FromKnownColor(KnownColor.ActiveCaption);
        private readonly string _searchToolTip = "开始搜索";
        private readonly string _clearKeyWordToolTip = "清除关键字，显示全部日记。";

        /// <summary>
        /// 获取或设置搜索关键字。
        /// </summary>
        public string KeyWord
        {
            get { return textBoxKeyWord.Text; }
            set { textBoxKeyWord.Text = value; }
        }

        /// <summary>
        /// 获取或设置是否为只读状态。
        /// </summary>
        public bool ReadOnly
        {
            get { return textBoxKeyWord.ReadOnly; }
            set { textBoxKeyWord.ReadOnly = value; }
        }


        public SearchTextBox()
        {
            InitializeComponent();

            DoubleBuffered = true;
            ResizeRedraw = true;

            btnSearch.ImageOnMouseDown = imageListSearch.Images[2];
            btnSearch.ImageOnMouseEnter = imageListSearch.Images[1];
            btnSearch.ImageOnNormal = imageListSearch.Images[0];

            toolTipMain.SetToolTip(btnSearch, _searchToolTip);
        }

        /// <summary>
        /// 开始搜索时触发此事件。
        /// </summary>
        public event EventHandler<OnSearchEventArgs> Search;

        protected virtual void OnSearch(object sender, OnSearchEventArgs e)
        {
            if (Search != null)
            {
                Search(sender, e);
            }
        }

        /// <summary>
        /// 重置搜索时触发此事件。
        /// </summary>
        public event EventHandler<EventArgs> ResetSearch;

        protected virtual void OnResetSearch(object sender, EventArgs e)
        {
            if (ResetSearch != null) { ResetSearch(sender, e); }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            //e.Graphics.DrawRectangle(_borderPen, this.ClientRectangle);

            //ControlPaint.DrawBorder(e.Graphics, ClientRectangle, _borderColor, ButtonBorderStyle.Solid);
            ControlPaint.DrawVisualStyleBorder(e.Graphics, ClientRectangle);
        }

        private void ExecuteSearchAction()
        {
            OnSearchEventArgs args = new OnSearchEventArgs();
            args.KeyWord = textBoxKeyWord.Text;

            OnSearch(this.textBoxKeyWord, args);
        }

        private enum SearchAction { Search, Clear }


        /// <summary>
        /// 更改搜索按钮图示。
        /// </summary>
        /// <param name="action"></param>
        private void ChangeSearchIcon(SearchAction action)
        {
            switch (action)
            {
                case SearchAction.Clear:
                    {
                        btnSearch.ImageOnMouseDown = imageListSearch.Images[5];
                        btnSearch.ImageOnMouseEnter = imageListSearch.Images[4];
                        btnSearch.ImageOnNormal = imageListSearch.Images[3];

                        btnSearch.Tag = "1";

                        toolTipMain.SetToolTip(btnSearch, _clearKeyWordToolTip);

                        break;
                    }
                case SearchAction.Search:
                    {
                        btnSearch.ImageOnMouseDown = imageListSearch.Images[2];
                        btnSearch.ImageOnMouseEnter = imageListSearch.Images[1];
                        btnSearch.ImageOnNormal = imageListSearch.Images[0];

                        btnSearch.Tag = "0";

                        toolTipMain.SetToolTip(btnSearch, _searchToolTip);

                        break;
                    }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (btnSearch.Tag.ToString() == "0") //显示清除关键字图示
            {
                ChangeSearchIcon(SearchAction.Clear);
            }
            else //显示搜索图示(重置搜索)
            {
                ChangeSearchIcon(SearchAction.Search);

                //清空关键字
                textBoxKeyWord.Clear();

                OnResetSearch(sender, EventArgs.Empty);
            }

            ExecuteSearchAction();
        }

        private void textBoxKeyWord_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (textBoxKeyWord.Text.Length == 0) { return; }

                ExecuteSearchAction();
                ChangeSearchIcon(SearchAction.Clear);
            }
        }

        private void textBoxKeyWord_TextChanged(object sender, EventArgs e)
        {
            if (textBoxKeyWord.Text.Length == 0)
            {
                ChangeSearchIcon(SearchAction.Search);
                ExecuteSearchAction();
            }

            btnSearch.Enabled = textBoxKeyWord.Text.Length > 0;
        }

        private void textBoxKeyWord_Enter(object sender, EventArgs e)
        {
            _borderColor = Color.Goldenrod;
            Invalidate();
        }

        private void textBoxKeyWord_Leave(object sender, EventArgs e)
        {
            _borderColor = Color.FromKnownColor(KnownColor.ActiveCaption);
            Invalidate();
        }

        private void textBoxKeyWord_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right || textBoxKeyWord.Text.Length == 0) { return; }

            if (textBoxKeyWord.SelectionLength == 0 &&
                textBoxKeyWord.SelectionStart == textBoxKeyWord.Text.Length)
            {
                textBoxKeyWord.SelectAll();
            }
        }
    }

    public class OnSearchEventArgs : EventArgs
    {
        private string _keyWord = string.Empty;

        /// <summary>
        /// 关键字。
        /// </summary>
        public string KeyWord
        {
            get { return _keyWord; }
            set { _keyWord = value; }
        }

        public OnSearchEventArgs() { }
    }
}
